﻿Imports TBCC.UtilityMobile
Public Class HDCarCurveForm

    Private projectId As String
    Private id As String
    Private projectName As String
    Private startTime As String
    Private endTime As String
    Private BeginAddress As String
    Private EndAddress As String
    Private Carrier As String

    Private _g As Graphics
    Private _gPrint As Graphics

    Private _bmp As Bitmap
    Private _bmpPrint As Bitmap

    Private _bgColor As Color = Color.Black
    Private _bgColor4Print As Color = Color.White

    Private _foreColor As Color = Color.White
    Private _foreColor4Print As Color = Color.Black

    Private _scaleColor As Color = Color.Gray

    Private _l As New List(Of RTDValueInfo) '用于存放冷库对应的端口信息
    Private _dl As New List(Of RTData) '用于存放端口在dtpF到dtpT这段时间端口对应的数据信息和端口信息

    Private Const SCALECOUNT As Integer = 5

    Dim aWidth As Integer = 720 '830 '880
    Dim aHeight As Integer = 500
    Dim aTop As Integer = 20
    Dim aLeft As Integer = 180 '120

    Dim hasTemp As Boolean
    Dim hasHumi As Boolean
    Dim isSelecting As Boolean = True

    Dim _forePen As New Pen(Color.White)
    Dim _forePen4Print As New Pen(Color.Black)

    Dim vTime(24 * 60) As String

    Private _foreBrush As New SolidBrush(Color.White)
    Private _foreBrush4Print As New SolidBrush(Color.Black)

    Private _timeInterval As Boolean = False '用于计算时间的间隔

    '温湿度上下限
    Private MAX_TEMP_LIMIT As Integer = 50
    Private MIN_TEMP_LIMIT As Integer = 0
    Private MAX_HUM_LIMIT As Integer = 100
    Private MIN_HUM_LIMIT As Integer = 0

    '**************************************************************
    '20090616 李吉鹏增加 为增加温湿度基准线及控制显示功能
    Private TEMP_UP_LINE As Integer = 50
    Private TEMP_DOWN_LINE As Integer = 0
    Private HUMI_UP_LINE As Integer = 100
    Private HUMI_DOWN_LINE As Integer = 0
    Private TEMP_UP_LINE_SHOW As Boolean = False
    Private TEMP_DOWN_LINE_SHOW As Boolean = False
    Private HUMI_UP_LINE_SHOW As Boolean = False
    Private HUMI_DOWN_LINE_SHOW As Boolean = False
    '*******************************************************

    '温湿度坐标区域
    Private _THC_Area As System.Drawing.Rectangle

    Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

    End Sub

    Sub New(ByVal projectId As String, ByVal id As String, ByVal projectName As String, ByVal startTime As String, ByVal endTime As String, ByVal BenginAddress As String, ByVal EndAddress As String, ByVal Carrier As String)
        MyClass.New()
        Me.projectId = projectId
        Me.id = id
        Me.projectName = projectName
        Me.startTime = startTime
        Me.endTime = endTime
        Me.BeginAddress = BenginAddress
        Me.EndAddress = EndAddress
        Me.Carrier = Carrier
    End Sub

    Private ddtpFrom As DateTime
    Private ddtpTo As DateTime
    'Private dddtpFrom As DateTime
    'Private dddtpTo As DateTime


    Public Sub Init()
        Me.txtProjectName.Text = projectName
        Me.dtpFrom.Text = startTime
        Me.ddtpFrom = String.Format("{0} {1}:00:00", Convert.ToDateTime(startTime).ToString("yyyy-MM-dd"), Convert.ToDateTime(startTime).Hour)
        'Me.dddtpFrom = String.Format("{0} {1}:{2}:00", Convert.ToDateTime(startTime).ToString("yyyy-MM-dd"), Convert.ToDateTime(startTime).Hour, Convert.ToDateTime(startTime).Minute)
        Me.dtpTo.Text = endTime
        If Me.dtpTo.Value.Second = 0 And Me.dtpTo.Value.Minute = 0 Then
            Me.ddtpTo = String.Format("{0} {1}:00:00", Me.dtpTo.Value.ToString("yyyy-MM-dd"), Me.dtpTo.Value.Hour)
        Else
            Me.ddtpTo = String.Format("{0} {1}:00:00", Me.dtpTo.Value.ToString("yyyy-MM-dd"), Me.dtpTo.Value.Hour + 1)
        End If
        ' Me.dddtpTo = String.Format("{0} {1}:{2}:00", Convert.ToDateTime(endTime).ToString("yyyy-MM-dd"), Convert.ToDateTime(endTime).Hour, Convert.ToDateTime(endTime).Minute + 1)
        Me.txtBeginAddress.Text = BeginAddress
        Me.txtEndAddress.Text = EndAddress
        Me.txtCarrier.Text = Carrier
        Me.txtStartStopTime.Text = CarHDStartUpService.GetStartStopSpace(projectId, id)

        Me.dtpFrom.CustomFormat = String.Format("yyyy-MM-dd HH:mm:{0}", dtpFrom.Value.Second)
        'Refresh_Curve()
        InitBG(_g)
    End Sub


    ''' <summary>
    ''' 初始化两个绘图图面,_g,_gPrint
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub InitBitmap()
        'Bitmap 是用于处理由像素数据定义的图像的对象,
        'Bitmap (Int32, Int32, PixelFormat)  用指定的大小和格式初始化 Bitmap 类的新实例
        ' width新 Bitmap 的宽度（以像素为单位）。
        ' Height新 Bitmap 的高度（以像素为单位）。
        ' Format()新 Bitmap 的 PixelFormat 枚举。
        _bmp = New Bitmap(1024, 570, System.Drawing.Imaging.PixelFormat.Format32bppArgb)

        '获取或设置 PictureBox 显示的图像
        Me.PictureBox1.Image = _bmp
        '从指定的 Image 创建新的 Graphics
        _g = Graphics.FromImage(_bmp)
        '清除整个绘图面并以指定背景色填充,此处以黑色填充
        _g.Clear(Color.Black)

        _bmpPrint = New Bitmap(1024, 570, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
        _gPrint = Graphics.FromImage(_bmpPrint)
        _gPrint.Clear(Color.White)
    End Sub

    Private Sub HDCarCurveForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.CenterToParent()
        'Me.ddtpTo = endTime   '在构造函数后执行
        ' Me.ddtpFrom = startTime

        InitBitmap() '初始化绘图图片
        LoadWareHouse() '初始化仓库下拉列表框
        'Me.WindowState = FormWindowState.Maximized

        'Me.btnRefresh.PerformClick()
        Me.labCur.BackColor = Color.FromArgb(65, 204, 212, 230)

        For i As Integer = 0 To 720
            AddKIL(i)
        Next
        KeyIndexMnN.AddLast(0)
        KeyIndexMnN.AddLast(720)
        Init()
    End Sub

    ''' <summary>
    ''' 加载仓库
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadWareHouse()
        'Dim rts As RefTypeService = New RefTypeService(projectId)

        'For Each rti As RefTypeItem In rts.GeRefGroupList(projectId)
        '    cboWarehouse.Items.Add(rti)
        'Next

        'If cboWarehouse.Items.Count >= 1 Then
        '    cboWarehouse.SelectedIndex = 0
        '    '  btnRefresh.Enabled = True
        '    btnSavePic.Enabled = True
        'Else
        '    cboWarehouse.SelectedIndex = -1
        '    '  btnRefresh.Enabled = False
        '    btnSavePic.Enabled = False
        'End If
        cboWarehouse_SelectedIndexChanged()
    End Sub

    Private Sub cboWarehouse_SelectedIndexChanged()
        Dim portnum As Integer = 0
        hasTemp = False
        hasHumi = False
        isSelecting = True

        _l.Clear() '清空RTDValueInfo泛型集合

        Dim Color_Zone() As Color = {Color.Red, Color.White, Color.SpringGreen, Color.MediumVioletRed, Color.MediumSlateBlue, Color.Snow, Color.GreenYellow, Color.OrangeRed, Color.Yellow, Color.DeepSkyBlue, Color.Cyan, Color.LightGray}

        Dim cbo_rti As CarRtiItemInfo = CarRtiItemService.GeRefGroupList(projectId).Item(0)


        MAX_TEMP_LIMIT = cbo_rti.HDCurveTempUp
        MIN_TEMP_LIMIT = cbo_rti.HDCurveTempDown
        MAX_HUM_LIMIT = cbo_rti.HDCurveHumiUp
        MIN_HUM_LIMIT = cbo_rti.HDCurveHumiDown

        '**************************************************************
        '20090616 李吉鹏增加 为增加温湿度基准线及控制显示功能
        TEMP_UP_LINE = cbo_rti.HDCurveTempLineUp
        TEMP_DOWN_LINE = cbo_rti.HDCurveTempLineDown
        HUMI_UP_LINE = cbo_rti.HDCurveHumiLineUp
        HUMI_DOWN_LINE = cbo_rti.HDCurveHumiLineDown
        TEMP_UP_LINE_SHOW = cbo_rti.HDCurveTempLineUpShow
        TEMP_DOWN_LINE_SHOW = cbo_rti.HDCurveTempLineDownShow
        HUMI_UP_LINE_SHOW = cbo_rti.HDCurveHumiLineUpShow
        HUMI_DOWN_LINE_SHOW = cbo_rti.HDCurveHumiLineDownShow
        '****************************************************************
        'Dim apts As AiPortTypeService = New AiPortTypeService(cbo_rti)
        'For Each apti As AiPortTypeItem In apts.GetAiGroupList()
        '    If String.IsNullOrEmpty(apti.PortName) = False AndAlso apti.PortNo <> 0 Then '如果AI名称不为空并且端口号不为0
        '        'portnum += 1
        '        If apti.DataType = 1 Then '温度
        '            _l.Add(CreateRTDValueInfo(apti.PortNo, apti.PortName, MAX_TEMP_LIMIT, MIN_TEMP_LIMIT, Color_Zone(apti.PortNo - 1), apti.DataType))
        '            If hasTemp = False Then
        '                portnum += 1
        '                hasTemp = True
        '            End If
        '        ElseIf apti.DataType = 2 Then '湿度
        '            _l.Add(CreateRTDValueInfo(apti.PortNo, apti.PortName, MAX_HUM_LIMIT, MIN_HUM_LIMIT, Color_Zone(apti.PortNo - 1), apti.DataType))
        '            If hasHumi = False Then
        '                portnum += 1
        '                hasHumi = True
        '            End If
        '        End If
        '    End If
        'Next

        _l.Add(CreateRTDValueInfo(1, "T1", MAX_TEMP_LIMIT, MIN_TEMP_LIMIT, Color.Red, 1))
        _l.Add(CreateRTDValueInfo(2, "T2", MAX_TEMP_LIMIT, MIN_TEMP_LIMIT, Color.White, 1))
        _l.Add(CreateRTDValueInfo(3, "T3", MAX_TEMP_LIMIT, MIN_TEMP_LIMIT, Color.SpringGreen, 1))
        _l.Add(CreateRTDValueInfo(4, "RH1", MAX_HUM_LIMIT, MIN_HUM_LIMIT, Color.MediumVioletRed, 2))
        hasTemp = True

        hasHumi = True

        '根据需要显示的温湿度的数量调整曲线的坐标
        aWidth = 720 '830
        aHeight = 500
        aTop = 20
        '  aLeft = 40 * portnum + 20
        aLeft = 40 * 2 + 20

        GetTHCArea() '设置温湿度显示的区域

        InitBG(_g) '刷新图层但不显示数据
        Me.PictureBox1.Image = _bmp '重新设置图片

        isSelecting = False
    End Sub

    '创建一个端口实例信息
    Private Function CreateRTDValueInfo(ByVal id As Integer, ByVal name As String, ByVal maxvalue As Integer, ByVal minvalue As Integer, ByVal c As Color, ByVal dataType As Integer) As RTDValueInfo
        Dim vi As New RTDValueInfo()
        vi.ID = id
        vi.Name = name
        vi.maxValue = maxvalue
        vi.minValue = minvalue
        vi.color = c
        vi.length = 2
        vi.dataType = dataType
        Return vi
    End Function

    'Private Function CreateRTDValueInfo(ByVal i As Integer) As RTDValueInfo
    '    Return CreateRTDValueInfo(i, Color.Red)
    'End Function

    'Private Function CreateRTDValueInfo(ByVal i As Integer, ByVal c As Color) As RTDValueInfo
    '    Dim strName As String = ""
    '    If i <= 16 Then
    '        If i <= 8 Then
    '            strName = "T" & i.ToString()
    '        Else
    '            strName = "H" & (i - 8).ToString()
    '        End If

    '        'If i Mod 2 = 1 Then
    '        '    strName = "温度" & ((i - 1) \ 2 + 1).ToString()
    '        'Else
    '        '    strName = "湿度" & ((i - 1) \ 2 + 1).ToString()
    '        'End If

    '    ElseIf i <= 25 Then
    '        strName = "TBCC_T" & (i - 16).ToString()

    '    ElseIf i <= 28 Then
    '        strName = "TBCC_H" & (i - 25).ToString()
    '    End If
    '    Return CreateRTDValueInfo(strName, 100, 0, c)
    'End Function

    'Private Function CreateRTDValueInfo(ByVal i As Integer, ByVal maxvalue As Integer, ByVal minvalue As Integer, ByVal c As Color) As RTDValueInfo
    '    Dim strName As String = ""
    '    If i <= 16 Then
    '        If i <= 8 Then
    '            strName = "T" & i.ToString()
    '        Else
    '            strName = "H" & (i - 8).ToString()
    '        End If
    '        'If i Mod 2 = 0 Then
    '        '    strName = "温度" & ((i - 1) \ 2 + 1).ToString()
    '        'Else
    '        '    strName = "湿度" & ((i - 1) \ 2 + 1).ToString()
    '        'End If
    '    ElseIf i <= 25 Then
    '        strName = "TBCC_T" & (i - 16).ToString()

    '    ElseIf i <= 28 Then
    '        strName = "TBCC_H" & (i - 25).ToString()
    '    End If


    '    Return CreateRTDValueInfo(i, strName, maxvalue, minvalue, c)

    'End Function

    ''' <summary>
    ''' 计算查询的时间间隔是否合法
    ''' </summary>
    ''' <returns>T or F</returns>
    ''' <remarks></remarks>
    Private Function IsValidated() As Boolean
        If Me.dtpFrom.Value < Convert.ToDateTime(startTime) Then
            msg.ShowWarning("起始时间超过起停记录开始时间!")
            Return False
        End If

        If Me.dtpTo.Value > Convert.ToDateTime(endTime) Then
            msg.ShowWarning("结束时间超过起停记录末尾时间!")
            Return False
        End If

        Dim dif As TimeSpan
        dif = Me.dtpTo.Value - Me.dtpFrom.Value

        If dif.TotalSeconds <= 0 Then
            msg.ShowWarning("请检查时间间隔!")
            Return False
        End If

        If dif.Hours < 6 Then
            _timeInterval = True
        Else
            _timeInterval = False
        End If

        'If dif.TotalHours < 0 OrElse dif.TotalHours > 8 * 24 Then
        '    dtpTo.Focus()
        '    msg.ShowWarning("查询间隔在0小时至8天之间。")
        '    Return False
        'End If

        Return True
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Refresh_Curve()
        If IsValidated() Then

            'If Me.btnRefresh.Text.Equals("刷新") Then
            InitBG(_g) '刷新图层但不显示数据

            '    ' RefreshData()
            Me.pnlSearch.Visible = True
            '    'Me.btnRefresh.Enabled = False
            '    Me.btnRefresh.Text = "取消"
            '    Me.cboWarehouse.Enabled = False
            '    Me.dtpFrom.Enabled = False
            '    Me.dtpTo.Enabled = False
            '    Me.btnSavePic.Enabled = False
            '    Me.btnTHCCfg.Enabled = False
            '    'Me.PictureBox1.Focus()
            Me.dtpFrom.Enabled = False
            Me.dtpTo.Enabled = False
            Me.btnShow.Enabled = False
            Me.btnTHCCfg.Enabled = False
            Me.btnSavePic.Enabled = False
            Me.btnClose.Enabled = False
            Try
                bwSearch.RunWorkerAsync()

            Catch ex As Exception

            End Try
            '    'RefreshImage()
            'Else
            '    Me.btnRefresh.Enabled = False
            '    bwSearch.CancelAsync()
            'End If

        End If
    End Sub

    Public grp As IList(Of Group) = New List(Of Group)

    Public Sub InitGroup(ByVal sql As String)
        grp.Clear()
        Dim dt As DataSet
        dt = SqlHelper.ExecuteDataset(sql, Nothing)
        Dim count = dt.Tables(0).Rows.Count

        For index As Integer = 0 To (count - 1)

            Dim g As New Group
            g.Space = dt.Tables(0).Rows(index)("space")
            g.StartTime = dt.Tables(0).Rows(index)("startTime")
            If index = 0 Then
                g.StartTime = Me.ddtpFrom
            End If
            g.EndTime = dt.Tables(0).Rows(index)("endTime")

            If index = (count - 1) Then
                g.EndTime = Me.ddtpTo
            End If

            grp.Add(g)
        Next index

        System.Diagnostics.Debug.WriteLine("分组： " + grp.Count.ToString)
    End Sub

    Public Function GetGroupIntenvel(ByVal i As Integer) As Double

        Dim length As Double = 0
        For index As Integer = 0 To i

            'If index = 0 Then
            '    length += (720 * ((grp(i).EndTime - grp(i).StartTime).TotalSeconds / allTime))

            'Else
            '    length += (720 * ((grp(i).EndTime - grp(i).StartTime).TotalSeconds / allTime / (1.5)))
            'End If
            length += (720 * ((grp(i).EndTime - grp(i).StartTime).TotalSeconds / allTime))
            System.Diagnostics.Debug.WriteLine("(grp(i).EndTime - grp(i).StartTime).TotalSeconds / allTime): " + ((grp(i).EndTime - grp(i).StartTime).TotalSeconds / allTime).ToString)
        Next
        System.Diagnostics.Debug.WriteLine("共有长度： " + length.ToString())
        Return length

    End Function


    Public Shared allTime As Double
    ''' <summary>
    ''' 刷新
    ''' </summary>
    ''' <param name="argument"></param>
    ''' <remarks></remarks>
    Private Sub RefreshData(ByVal argument As Object)
        _dl.Clear()
        KeyIndexList.Clear()
        KeyIndexMnN.Clear()
        IsFirst = True

        Dim _lt As New List(Of RTDValueInfo)
        _lt.Clear()
        For i As Integer = 0 To vTime.Length - 1
            vTime(i) = Nothing
        Next

        Dim strSql As String
        '  Dim cbo_rti As RefTypeItem = argument(0) 'CType(cboWarehouse.SelectedItem, RefTypeItem)
        Dim dtF As DateTime = Me.ddtpFrom
        Dim dtT As DateTime = Me.ddtpTo
        Select Case (Me.dtpTo.Value - Me.dtpFrom.Value).TotalSeconds
            Case Is >= 3600
                dtF = Me.ddtpFrom
                dtT = Me.ddtpTo
            Case Else
                dtF = Me.dtpFrom.Value
                dtT = Me.dtpTo.Value
        End Select

        Dim Space As Integer = CarHDStartUpService.GetStartStopSpace(projectId, id)

        allTime = (dtT - dtF).TotalSeconds
        '{2}%00
        strSql = String.Format("select * from tbcchistdata_car_{0}_1 where parentId={1} and (UpdateTime >='{2}' and UpdateTime <= '{3}')", _
            Me.projectId, _
            Me.id, _
            UtilityClass.GetDateString(dtF), _
            UtilityClass.GetDateString(dtT) _
        )

        'Dim strSql2 As String = String.Format("select space,startTime,endTime from tbcchddatastartendpage where (endTime >='{0}' and startTime <= '{1}') or (startTime <='{1}' and endTime = '0001-01-01 01:01:01') ", _
        'UtilityClass.GetDateString(dtF), _
        'UtilityClass.GetDateString(dtT) _
        '    )

        Dim ds As DataSet
        ds = SqlHelper.ExecuteDataset(strSql, Nothing)

        Dim count As Integer
        count = ds.Tables(0).Rows.Count

        Dim dt As DateTime
        Dim lastdt As DateTime
        Dim tempdt As DateTime
        Dim im As Decimal


        'InitGroup(strSql2)

        '_lt.Add(CreateRTDValueInfo(1, "T1", 50, 0, Color.Red, 1))
        '_lt.Add(CreateRTDValueInfo(2, "T2", 50, 0, Color.White, 1))
        '_lt.Add(CreateRTDValueInfo(3, "T3", 50, 0, Color.SpringGreen, 1))
        '_lt.Add(CreateRTDValueInfo(4, "RH1", 50, 0, Color.MediumVioletRed, 2))

        '     Dim countGrp As Integer = grp.Count

        If count > 0 Then


            '便利端口，为端口选择720点来绘图
            For Each vi As RTDValueInfo In _l 'vi代表一个端口


                Dim v(24 * 60) As Single '定义v数组，长度为24*60=720  用于存放端口720个点存放数据对象
                'Dim v(24 * 120) As Single
                For j As Integer = 0 To v.Length - 1
                    v(j) = Single.NaN '初始化为0
                Next
                v(0) = 0.0

                Select Case vi.ID
                    Case 1
                        If Me.CheckBox1.Checked = False Then
                            Dim item1 As New RTData()
                            item1._bufSingle = v 'DLine(v, 0) 'v代表端口对应的720个点的温湿度数据

                            item1.rtdItem = vi '声明数据item是属于哪个端口的

                            _dl.Add(item1) '加载到端口对象（加载自己）
                            Continue For
                        End If
                    Case 2
                        If Me.CheckBox2.Checked = False Then
                            Dim item1 As New RTData()
                            item1._bufSingle = v 'DLine(v, 0) 'v代表端口对应的720个点的温湿度数据

                            item1.rtdItem = vi '声明数据item是属于哪个端口的

                            _dl.Add(item1) '加载到端口对象（加载自己）
                            Continue For

                        End If
                    Case 3
                        If Me.CheckBox3.Checked = False Then
                            Dim item1 As New RTData()
                            item1._bufSingle = v 'DLine(v, 0) 'v代表端口对应的720个点的温湿度数据

                            item1.rtdItem = vi '声明数据item是属于哪个端口的

                            _dl.Add(item1) '加载到端口对象（加载自己）
                            Continue For

                        End If
                    Case 4
                        If Me.CheckBox4.Checked = False Then
                            Dim item1 As New RTData()
                            item1._bufSingle = v 'DLine(v, 0) 'v代表端口对应的720个点的温湿度数据

                            item1.rtdItem = vi '声明数据item是属于哪个端口的

                            _dl.Add(item1) '加载到端口对象（加载自己）
                            Continue For
                        End If
                End Select

                Dim proIndex As Integer = 0
                Dim i As Integer = 0
                '便利组
                Dim f As Integer = 0
                ' For index As Integer = 0 To countGrp - 1


                ' If countGrp = 1 Then
                im = ((dtT - dtF).TotalSeconds / Space) / 720  'INTERVALSECOND 指2次实时数据之间的间隔时间(秒)，im得到720个间隔每一个间隔多少条数据
                '  System.Diagnostics.Debug.WriteLine("数据总条数: " + ((dtT - dtF).TotalSeconds / grp(index).Space).ToString + "  " + vi.ID.ToString())
                ' System.Diagnostics.Debug.WriteLine("数据所占等份: " + (((720 * (((dtT - dtF).TotalSeconds / (dtT - dtF).TotalSeconds))))).ToString)
                'Else
                ''im = ((grp(index).EndTime - grp(index).StartTime).TotalSeconds / grp(index).Space) / GetGroupIntenvel(index) '((720 * (grp(index).EndTime - grp(index).StartTime).TotalSeconds / (dtT - dtF).TotalSeconds))  'INTERVALSECOND 指2次实时数据之间的间隔时间(秒)，im得到720个间隔每一个间隔多少条数据
                'im = ((grp(index).EndTime - grp(index).StartTime).TotalSeconds / grp(index).Space) / ((720 * (grp(index).EndTime - grp(index).StartTime).TotalSeconds / (dtT - dtF).TotalSeconds))  'INTERVALSECOND 指2次实时数据之间的间隔时间(秒)，im得到720个间隔每一个间隔多少条数据
                'System.Diagnostics.Debug.WriteLine("数据总条数: " + ((grp(index).EndTime - grp(index).StartTime).TotalSeconds / grp(index).Space).ToString + "  " + vi.ID.ToString())
                'System.Diagnostics.Debug.WriteLine("开始时间: " + grp(index).StartTime.ToString)
                'System.Diagnostics.Debug.WriteLine("结束时间: " + grp(index).EndTime.ToString)
                'System.Diagnostics.Debug.WriteLine("数据所占等份: " + (((720 * (grp(index).EndTime - grp(index).StartTime).TotalSeconds / (dtT - dtF).TotalSeconds).ToString)))

                '' If index > 0 Then
                ''End If
                'End If

                '''''''''''''''''''''''''新的补点方案代码 Part.1 BEGIN
                Dim dddd As Integer = (Space / (dtT - dtF).TotalSeconds) * 720
                If dddd = 0 Then
                    dddd = 1
                End If
                Dim last As Integer = -1
                '''''''''''''''''''''''''新的补点方案代码 Part.1 END

                For Each r As DataRow In ds.Tables(0).Rows   'r代表端口存放数据
                    If Me.bwSearch.CancellationPending Then '取消查询
                        ds.Clear() : ds.Dispose() : ds = Nothing
                        Exit Sub
                    End If
                    dt = r("UpdateTime")
                    'If dt.Second <> 0 Then '非整分钟
                    '    Continue For ./contacts
                    'End If

                    'i = (dt - dtF).TotalSeconds / 10
                    If dt < Me.dtpFrom.Value Or dt > Me.dtpTo.Value Then
                        Continue For
                    End If

                    '  If dt >= grp(index).StartTime AndAlso dt <= grp(index).EndTime Then

                    Dim fd As DateTime
                    If IsFirst = True Then
                        f = ((dt - dtF).TotalSeconds / (dtT - dtF).TotalSeconds) * 720
                        fd = dt
                        IsFirst = False

                    End If

                    'If countGrp = 1 Then
                    i = (dt - dtF).TotalSeconds / Space
                    '  Debug.WriteLine(String.Format("{0} = ({1} - {2}).TotalSeconds / {3}", i, dt, Me.dtpFrom.Text, Space))
                    'Else
                    '    If index = 0 Then
                    '        i = (dt - grp(index).StartTime).TotalSeconds / grp(index).Space 'INTERVALSECOND 指2次实时数据之间的间隔时间(秒)，i指当前时间的条数
                    '    Else
                    '        ' i = (dt - grp(index).StartTime).TotalSeconds / grp(index).Space 'INTERVALSECOND 指2次实时数据之间的间隔时间(秒)，i指当前时间的条数
                    '        i = (dt - fd).TotalSeconds / grp(index).Space 'INTERVALSECOND 指2次实时数据之间的间隔时间(秒)，i指当前时间的条数

                    '        '   i = (dt - lastdt).TotalSeconds / grp(index).Space
                    '    End If

                    'End If
                    ' i = (dt - grp(index).StartTime).TotalSeconds / grp(index).Space

                    'If Math.Round(i Mod im) <> 0 Then   '当前条数/总条数*720取模   1-720之间
                    '    Continue For
                    'End If

                    'If index = 0 Then
                    i = i / im
                    'Else
                    '    i = i / im + f
                    'End If

                    '测试用 检查为什么最后一个点不会被绘制
                    'If i > 700 Then
                    '    i = i
                    'End If

                    If r(String.Format("AI{0}", vi.ID.ToString())) <> -300 Then
                        v(i) = r(String.Format("AI{0}", vi.ID.ToString()))
                        vTime(i) = Convert.ToDateTime(r("UpdateTime")).TimeOfDay.ToString()
                    Else
                        '''''''''''''''''''''''''新的补点方案代码 Part.2 BEGIN
                        v(i) = Single.NaN
                        last = -1

                        '''''''''''''''''''''''''新的补点方案代码 Part.2 End
                        Continue For
                    End If
                    '''''''''''''''''''''''''新的补点方案代码 Part.3 BEGIN
                    If last = -1 Then
                        last = i
                    Else
                        If i - last > dddd * 1.5 Then
                            last = i
                        Else
                            P2Line(v, last, i)
                            last = i
                        End If
                    End If

                    '''''''''''''''''''''''''新的补点方案代码 Part.3 END
                    'If IsFirst = True Then
                    '    If KeyIndexList.Count = 0 Then
                    '        AddKIL(i)
                    '    ElseIf KeyIndexList.Last.Value <> i Then
                    '        AddKIL(i)
                    '    End If
                    'End If
                    IsKeyIndex(i)
                    'If index = 0 And i = 0 And IsFirst = True Then
                    '    proIndex += 1
                    '    tempdt = r("hDate")
                    '    IsFirst = False
                    '    Continue For
                    'End If
                    'If i = 0 Then
                    '    i = i
                    'End If
                    'If KeyIndexList.Count <> 0 Then
                    '    If Not KeyIndexList.Contains(i) Then
                    '        If i > KeyIndexList.Last.Value Then
                    '            AddKIL(i)
                    '        Else
                    '            Dim tk As New LinkedListNode(Of Integer)(KeyIndexList.First.Value)
                    '            For Each t As Integer In KeyIndexList
                    '                If t > i Then
                    '                    tk = KeyIndexList.Find(t)
                    '                    KeyIndexList.AddBefore(tk, i)
                    '                End If
                    '            Next
                    '        End If
                    '    End If
                    'Else
                    '    AddKIL(i)
                    'End If
                    proIndex += 1
                    tempdt = r("UpdateTime")
                    'End If

                    ReportProgress((proIndex / ds.Tables(0).Rows.Count) * 100) '进度报告
                Next
                lastdt = tempdt
                '  v = DLine(v, f)
                'f = i
                IsFirst = True
                ' Next



                Dim item As New RTData()
                item._bufSingle = v 'DLine(v, 0) 'v代表端口对应的720个点的温湿度数据

                item.rtdItem = vi '声明数据item是属于哪个端口的

                _dl.Add(item) '加载到端口对象（加载自己）


            Next
            '在这个循环第一次执行时将有数据的位置添加进入KeyIndexList队列
            If KeyIndexList.Count <> 0 Then
                KeyIndexMnN.AddLast(KeyIndexList.First.Value)
                KeyIndexMnN.AddLast(KeyIndexList.Last.Value)
            End If
        Else
            For i As Integer = 0 To 720
                AddKIL(i)
            Next
            KeyIndexMnN.AddLast(0)
            KeyIndexMnN.AddLast(720)
        End If

    End Sub

    Private Sub IsKeyIndex(ByVal i As Integer)
        If i = 608 Then
            i = i
        End If
        If KeyIndexList.Count <> 0 Then
            If Not KeyIndexList.Contains(i) Then
                If i > KeyIndexList.Last.Value Then
                    AddKIL(i)
                Else
                    Dim tk As New LinkedListNode(Of Integer)(KeyIndexList.First.Value)
                    For Each t As Integer In KeyIndexList
                        If t > i Then
                            tk = KeyIndexList.Find(t)
                            KeyIndexList.AddBefore(tk, i)
                            Exit For
                        End If
                    Next
                End If
            End If
        Else
            AddKIL(i)
        End If
    End Sub

    '以前的补点方案
    'Private Function DLine(ByVal v() As Single, ByVal pos As Integer) As Single()
    '    Dim low, high, f As Single
    '    Dim y, t As Single
    '    Dim x As Integer
    '    If pos = 0 Then '确定补点的起点
    '        x = pos
    '    Else : x = pos + 1 '由于pos是上次最后一个点的位置,因此如果不是从0开始就要从上次的后一个点开始
    '    End If
    '    If v(x).Equals(Single.NaN) Or v(x) = 0.0 Then
    '        Do
    '            x += 1
    '        Loop While v(x).Equals(Single.NaN) And x <> 720
    '    End If
    '    low = v(x)
    '    f = x
    '    y = low
    '    For i As Integer = x + 1 To v.Length - 1
    '        If v(i).Equals(Single.NaN) Then
    '            Continue For
    '        Else
    '            high = v(i)

    '            t = (high - low) / (i - f)
    '            For j As Integer = 0 To i - f - 1
    '                v(f + j) = t * j + y
    '            Next
    '            low = high
    '            f = i
    '            y = low
    '        End If
    '    Next
    '    Return v
    'End Function

    ''' <summary>
    ''' 根据两条记录的数据计算斜率,填充数据间空缺位置
    ''' </summary>
    ''' <param name="v">数组,有数据的部分表示一条记录</param>
    ''' <returns>填充过后的数组v</returns>
    ''' <remarks></remarks>
    Private Function P2Line(ByVal v() As Single, ByVal st As Integer, ByVal en As Integer) As Single()
        Dim t As Single
        t = (v(en) - v(st)) / (en - st)
        For j As Integer = 1 To en - st - 1
            v(st + j) = t * j + v(st)
        Next
        Return v
    End Function


    Private Sub RefreshImage()
        'InitBG(_g)
        DrawLine(_g)
        Me.PictureBox1.Image = _bmp
    End Sub

    Private Sub RefreshImage4Print()
        InitBG4Print(_gPrint)
        DrawLine4Print(_gPrint)
        'Me.PictureBox1.Image = _bmpPrint
    End Sub

    ''' <summary>
    ''' 初始化背景
    ''' </summary>
    ''' <param name="g"></param>
    ''' <remarks></remarks>
    Private Sub InitBG(ByVal g As Graphics)
        Me.labLine.Visible = False
        Me.labInfo.Visible = False

        'Me.TempUpLine.Visible = False
        g.Clear(_bgColor) '清除整个绘图面并以黑色做为背景色填充
        DrawMark(g) '在所有端口绘图矩形，作为标记
        DrawCoordinates(g) '确定要绘画的正方形位置 如：x,y坐标
        DrawXCoordinate(g, False) '设置X坐标
        DrawYCoordinate(g) '设置Y坐标
        SetCurLabPara() '设置冷库参数
        SetInfoLabPara() '设置冷库对应所有端口参数
    End Sub

    '**************************************************************
    '20090618李吉鹏修改 颜色列表框

    Private Sub DrawMark(ByVal g As Graphics)
        Dim markTop As Integer = 25
        Dim height As Integer = 25
        Dim left As Integer = aLeft + aWidth + 40

        'Dim rMark As New Rectangle(left, aTop, 120, height * _l.Count)
        Dim rMark As New Rectangle(left + 50, aTop, 80, height * _l.Count)
        g.DrawRectangle(_forePen, rMark)

        Dim font As Font = New Font(Me.Font.FontFamily, 10)

        Dim iRow As Integer = 0
        For Each item As RTDValueInfo In _l
            Dim r As New Rectangle(left + 55, iRow * height + markTop + 1, 12, 12)
            Dim b As Brush = New SolidBrush(item.color)
            '填充指定的矩形ｒ的内部，ｂ是确定填充特性的 Brush ，SolidBrush定义单色画笔。画笔用于填充图形形状，如矩形、椭圆、扇形、多边形和封闭路径
            g.FillRectangle(b, r)
            Dim s As String
            s = item.Name
            '在指定位置并且用指定的 Brush 和 Font 对象绘制指定的文本字符串。 
            's要绘制的字符串。 
            'Font，它定义字符串的文本格式。 
            'Brush，它确定所绘制文本的颜色和纹理。 
            'x所绘制文本的左上角的 x 坐标。 
            'y所绘制文本的左上角的 y 坐标。
            g.DrawString(s, font, _foreBrush, left + 80, iRow * height + markTop)
            iRow += 1
        Next
    End Sub

    Private Sub DrawCoordinates(ByVal g As Graphics)
        Dim r As New Rectangle(aLeft, aTop, aWidth, aHeight)
        '绘制由 Rectangle 结构指定的矩形
        '_forPen确定矩形的颜色、宽度和样式
        '绘制r指定的矩形
        g.DrawRectangle(_forePen, r)

        'g.DrawLine(_forePen, aLeft, aTop + aHeight \ 2, aLeft + aWidth, aTop + aHeight \ 2)
        '绘制一条连接由坐标对指定的两个点的线条
        '_forePen确定线条的颜色、宽度和样式。 
        'x1第一个点的 x 坐标。 
        'y1第一个点的 y 坐标。 
        'x2第二个点的 x 坐标。 
        'y2第二个点的 y 坐标。 
        g.DrawLine(_forePen, aLeft + aWidth, aTop + aHeight, aLeft + aWidth + 15, aTop + aHeight)
        '使用指定的填充模式填充 Point 结构指定的点数组所定义的多边形的内部,数组表示要填充的多边形的顶点
        g.FillPolygon(New SolidBrush(_forePen.Color), New Point() {New Point(aLeft + aWidth + 15, aTop + aHeight), _
                                                                New Point(aLeft + aWidth + 5, aTop + aHeight + 3), _
                                                                New Point(aLeft + aWidth + 5, aTop + aHeight - 3) _
                                                    } _
                            )
    End Sub

    '画出 X 坐标
    Private Sub DrawXCoordinate(ByVal g As Graphics, ByVal isPrint As Boolean)
        'If Me._dl.Count = 0 Then
        '    Exit Sub
        'End If

        'Dim index As Integer
        'index = Me._dl(0).CurrentIndex
        Dim tWidth As Single = 30
        Dim fb As Brush = _foreBrush
        If isPrint Then
            fb = _foreBrush4Print
        End If

        Dim dt As DateTime
        dt = Me.GetCurrentTime '获取终止时间
        'Pen定义用于绘制直线和曲线的对象
        Dim p As New Pen(Brushes.Gray, 2)
        Dim p1 As New Pen(Brushes.DarkGray, 2)

        Dim sTime As String

        Dim dtT, dtF As DateTime
        Dim im As Decimal = 1
        Dim base, length As Integer
        base = 1

        Select Case (Me.dtpTo.Value - Me.dtpFrom.Value).TotalSeconds
            Case Is >= 3600
                dtT = Me.ddtpTo
                dtF = Me.ddtpFrom
                length = (dtT - dtF).TotalSeconds \ 3600

                Dim last As Integer = 720 * (((dtT - dtF).TotalSeconds Mod 3600) / (dtT - dtF).TotalSeconds)

                If length > 0 Then
                    base = Math.Ceiling(length / 24)
                    If base > 1 AndAlso base Mod 2 <> 0 Then base += 1
                    im = 24 / length
                    tWidth = (720 - last) / length
                End If

                Dim pos As Integer = GetX(0) - 15
                Dim Interval As Integer = 43

                For i As Integer = 0 To length
                    'If Math.Round(i Mod im) = 0 Then
                    If (dtF.Hour + i) Mod base = 0 Then
                        If i <> 0 AndAlso i <> length Then
                            Select Case length
                                Case Is > 12
                                    sTime = ((dtF.Hour + i) Mod 24).ToString("D2") 'Math.Round(i / im)
                                Case Else
                                    sTime = ((dtF.Hour + i) Mod 24).ToString("D2") + ":00" 'Math.Round(i / im)
                            End Select
                            If GetX(i * tWidth) - 15 - pos > Interval Then
                                g.DrawString(sTime, Me.Font, fb, GetX(i * tWidth) - 15, aTop + aHeight + 5)
                                pos = GetX(i * tWidth) - 15
                            End If
                        End If

                        If ((dtF.Hour + i) Mod 24) = 0 Then
                            p1.Width = 2
                            Select Case length
                                Case Is > 12
                                    g.DrawLine(p1, GetX(i * tWidth), aTop, GetX(i * tWidth), aTop + aHeight)
                                Case Is >= 6
                                    g.DrawLine(p1, GetX(i * tWidth), aTop, GetX(i * tWidth), aTop + aHeight)
                                    If i <> length Then
                                        Dim w As Integer = i * tWidth + (tWidth / 2)
                                        p1.Width = 1
                                        g.DrawLine(p1, GetX(w), aTop, GetX(w), aTop + aHeight)
                                        If GetX(w) - 15 - pos > Interval Then
                                            sTime = ((dtF.Hour + i) Mod 24).ToString("D2") + ":30" 'Math.Round(i / im)
                                            g.DrawString(sTime, Me.Font, fb, GetX(w) - 15, aTop + aHeight + 5)
                                            pos = GetX(w) - 15
                                        End If
                                    End If
                                Case Is > 2
                                    g.DrawLine(p1, GetX(i * tWidth), aTop, GetX(i * tWidth), aTop + aHeight)
                                    If i <> length Then
                                        p1.Width = 1
                                        For st As Integer = 1 To 3
                                            Dim w As Integer = i * tWidth + (tWidth * st / 4)
                                            g.DrawLine(p1, GetX(w), aTop, GetX(w), aTop + aHeight)
                                            sTime = ((dtF.Hour + i) Mod 24).ToString("D2") + ":" + (st * 15).ToString() 'Math.Round(i / im)
                                            g.DrawString(sTime, Me.Font, fb, GetX(w) - 15, aTop + aHeight + 5)
                                        Next
                                    End If
                                Case Is > 1
                                    g.DrawLine(p1, GetX(i * tWidth), aTop, GetX(i * tWidth), aTop + aHeight)
                                    If i <> length Then
                                        p1.Width = 1
                                        For st As Integer = 1 To 5
                                            Dim w As Integer = i * tWidth + (tWidth * st / 6)
                                            g.DrawLine(p1, GetX(w), aTop, GetX(w), aTop + aHeight)
                                            sTime = ((dtF.Hour + i) Mod 24).ToString("D2") + ":" + (st * 10).ToString() 'Math.Round(i / im)
                                            g.DrawString(sTime, Me.Font, fb, GetX(w) - 15, aTop + aHeight + 5)
                                        Next
                                    End If
                                Case Else
                                    g.DrawLine(p1, GetX(i * tWidth), aTop, GetX(i * tWidth), aTop + aHeight)
                                    If i <> length Then
                                        p1.Width = 1
                                        For st As Integer = 1 To 11
                                            Dim w As Integer = i * tWidth + (tWidth * st / 12)
                                            g.DrawLine(p1, GetX(w), aTop, GetX(w), aTop + aHeight)
                                            sTime = ((dtF.Hour + i) Mod 24).ToString("D2") + ":" + (st * 5).ToString("D2") 'Math.Round(i / im)
                                            g.DrawString(sTime, Me.Font, fb, GetX(w) - 15, aTop + aHeight + 5)
                                        Next
                                    End If

                            End Select
                        Else
                            p.Width = 1
                            Select Case length
                                Case Is > 12
                                    g.DrawLine(p, GetX(i * tWidth), aTop, GetX(i * tWidth), aTop + aHeight)
                                Case Is >= 6
                                    g.DrawLine(p, GetX(i * tWidth), aTop, GetX(i * tWidth), aTop + aHeight)
                                    If i <> length Then
                                        Dim w As Integer = i * tWidth + (tWidth / 2)
                                        g.DrawLine(p, GetX(w), aTop, GetX(w), aTop + aHeight)
                                        If GetX(w) - 15 - pos > Interval Then
                                            sTime = ((dtF.Hour + i) Mod 24).ToString("D2") + ":30"
                                            g.DrawString(sTime, Me.Font, fb, GetX(w) - 15, aTop + aHeight + 5)
                                            pos = GetX(w) - 15
                                        End If
                                    End If
                                Case Is > 2
                                    g.DrawLine(p, GetX(i * tWidth), aTop, GetX(i * tWidth), aTop + aHeight)
                                    If i <> length Then
                                        p.Width = 1
                                        For st As Integer = 1 To 3
                                            Dim w As Integer = i * tWidth + (tWidth * st / 4)
                                            g.DrawLine(p, GetX(w), aTop, GetX(w), aTop + aHeight)
                                            sTime = ((dtF.Hour + i) Mod 24).ToString("D2") + ":" + (st * 15).ToString() 'Math.Round(i / im)
                                            g.DrawString(sTime, Me.Font, fb, GetX(w) - 15, aTop + aHeight + 5)
                                        Next
                                    End If
                                Case Is > 1
                                    g.DrawLine(p, GetX(i * tWidth), aTop, GetX(i * tWidth), aTop + aHeight)
                                    If i <> length Then
                                        p.Width = 1
                                        For st As Integer = 1 To 5
                                            Dim w As Integer = i * tWidth + (tWidth * st / 6)
                                            g.DrawLine(p, GetX(w), aTop, GetX(w), aTop + aHeight)
                                            sTime = ((dtF.Hour + i) Mod 24).ToString("D2") + ":" + (st * 10).ToString() 'Math.Round(i / im)
                                            g.DrawString(sTime, Me.Font, fb, GetX(w) - 15, aTop + aHeight + 5)
                                        Next
                                    End If
                                Case Else
                                    g.DrawLine(p, GetX(i * tWidth), aTop, GetX(i * tWidth), aTop + aHeight)
                                    If i <> length Then
                                        p.Width = 1
                                        For st As Integer = 1 To 11
                                            Dim w As Integer = i * tWidth + (tWidth * st / 12)
                                            g.DrawLine(p, GetX(w), aTop, GetX(w), aTop + aHeight)
                                            sTime = ((dtF.Hour + i) Mod 24).ToString("D2") + ":" + (st * 5).ToString("D2") 'Math.Round(i / im)
                                            g.DrawString(sTime, Me.Font, fb, GetX(w) - 15, aTop + aHeight + 5)
                                        Next
                                    End If
                            End Select

                        End If
                    End If

                    If i = 0 Then
                        'If (dtF.Hour Mod base) <= base / 2 Then
                        '  sTime = ((dtF.Hour + i) Mod 24).ToString("D2") 'Math.Round(i / im)
                        Select Case length
                            Case Is > 12
                                sTime = ((dtF.Hour + i) Mod 24).ToString("D2") 'Math.Round(i / im)
                            Case Else
                                sTime = ((dtF.Hour + i) Mod 24).ToString("D2") + ":00" 'Math.Round(i / im)
                        End Select
                        g.DrawString(sTime, Me.Font, fb, GetX(i * tWidth) - 15, aTop + aHeight + 5)
                        pos = GetX(0) - 15
                        'End If
                        p.Width = 1
                        g.DrawLine(p, GetX(i * tWidth), aTop, GetX(i * tWidth), aTop + aHeight)
                        If (24 - dtF.Hour) * tWidth >= 90 Then
                            g.DrawString(dtF.ToLongDateString, Me.Font, fb, GetX(i * tWidth) - 50, aTop + aHeight + 20)
                        End If
                    ElseIf ((dtF.Hour + i) Mod 24) = 0 Then
                        g.DrawString(dtF.AddDays(i / 24).ToLongDateString, Me.Font, fb, GetX(i * tWidth) - 50, aTop + aHeight + 20)
                    End If

                    If i = length Then
                        p.Width = 1
                        g.DrawLine(p, GetX(720), aTop, GetX(720), aTop + aHeight)
                        'sTime = ((dtF.Hour + i) Mod 24).ToString("D2") 'Math.Round(i / im)
                        'If (dtF.Hour + i) Mod base = 0 Then
                        '    g.DrawString(sTime, Me.Font, _foreBrush, GetX(720) - 8, aTop + aHeight + 5)
                        'Else
                        Select Case length
                            Case Is > 12
                                sTime = ((dtF.Hour + i) Mod 24).ToString("D2") 'Math.Round(i / im)
                            Case Else
                                sTime = ((dtF.Hour + i) Mod 24).ToString("D2") + ":00" 'Math.Round(i / im)
                        End Select
                        g.DrawString(sTime, Me.Font, fb, GetX(i * tWidth) - 15, aTop + aHeight + 5)
                        'End If
                        Select Case length
                            Case Is > 12
                                g.DrawString(String.Format("小时(×{0})", base), Me.Font, Brushes.Gray, GetX(750), aTop + aHeight + 5)
                            Case Is >= 6
                                g.DrawString("分钟(×30)", Me.Font, Brushes.Gray, GetX(750), aTop + aHeight + 5)
                            Case Is > 2
                                g.DrawString("分钟(×15)", Me.Font, Brushes.Gray, GetX(750), aTop + aHeight + 5)
                            Case Is > 1
                                g.DrawString("分钟(×10)", Me.Font, Brushes.Gray, GetX(750), aTop + aHeight + 5)
                            Case Else
                                g.DrawString("分钟(×05)", Me.Font, Brushes.Gray, GetX(750), aTop + aHeight + 5)
                        End Select
                    End If
                Next
            Case Else
                dtT = Me.dtpTo.Value
                dtF = Me.dtpFrom.Value
                Select Case (dtT - dtF).TotalSeconds
                    Case Is >= 2700
                        length = (dtT - dtF).TotalSeconds \ 240
                    Case Is >= 1800
                        length = (dtT - dtF).TotalSeconds \ 120
                    Case Is >= 600
                        length = (dtT - dtF).TotalSeconds \ 60
                    Case Else
                        length = (dtT - dtF).TotalSeconds \ 30
                End Select

                If length > 0 Then
                    base = Math.Ceiling(length / 24)
                    If base > 1 AndAlso base Mod 2 <> 0 Then base += 1
                    im = 24 / length
                    Select Case (dtT - dtF).TotalSeconds
                        Case Is >= 2700
                            tWidth = 720 / ((dtT - dtF).TotalSeconds / 240)
                        Case Is >= 1800
                            tWidth = 720 / ((dtT - dtF).TotalSeconds / 120)
                        Case Is >= 600
                            tWidth = 720 / ((dtT - dtF).TotalSeconds / 60)
                        Case Else
                            tWidth = 720 / ((dtT - dtF).TotalSeconds / 30)
                    End Select
                    ' tWidth = 720 / ((dtT - dtF).TotalSeconds / 120)
                End If

                Dim flag As Boolean = True
                Dim pos As Integer

                For i As Integer = 0 To length
                    p.Width = 1
                    g.DrawLine(p, GetX(i * tWidth), aTop, GetX(i * tWidth), aTop + aHeight)
                    Dim time As DateTime
                    Select Case (dtT - dtF).TotalSeconds
                        Case Is >= 2700
                            time = dtF.AddSeconds(240 * i)
                        Case Is >= 1800
                            time = dtF.AddSeconds(120 * i)
                        Case Is >= 600
                            time = dtF.AddSeconds(60 * i)
                        Case Else
                            time = dtF.AddSeconds(30 * i)
                    End Select
                    sTime = time.ToString("HH:mm:ss")
                    ' Dim x As Integer = 500 + Math.Sqrt(((i * tWidth) * (i * tWidth)) / 2)
                    'Dim y As Integer = 85 - Math.Sqrt(((i * tWidth) * (i * tWidth)) / 2)
                    Dim x As Integer
                    Dim y As Integer
                    'x = 346 + Math.Sqrt((i * tWidth * i * tWidth) - ((i * tWidth / 2) * (i * tWidth / 2)))
                    ' y = 400 - i * tWidth / 2
                    ' g.RotateTransform(30)
                    x = GetX(i * tWidth) - 15
                    y = aTop + aHeight + 5
                    If flag = True Then
                        pos = x
                        g.DrawString(sTime, Me.Font, fb, x, y)
                        flag = False
                        Continue For
                    End If
                    If x - pos > 50 Then
                        p1.Width = 3
                        g.DrawLine(p1, GetX(i * tWidth), aTop + 490, GetX(i * tWidth), aTop + aHeight)
                        g.DrawString(sTime, Me.Font, fb, x, y)
                        pos = x
                    End If
                    ' g.RotateTransform(-30)
                Next

                If Not dtT.Date.ToString().Equals(dtF.Date.ToString()) Then
                    pos = 720 * ((dtT.Date - dtF).TotalSeconds / (dtT - dtF).TotalSeconds)
                    p1.Width = 2
                    g.DrawLine(p1, GetX(0), aTop, GetX(0), aTop + aHeight)
                    g.DrawString(dtF.ToLongDateString, Me.Font, fb, GetX(0), aTop + aHeight + 20)
                    g.DrawLine(p1, GetX(0) + pos, aTop, GetX(0) + pos, aTop + aHeight)
                    g.DrawString(dtT.ToLongDateString, Me.Font, fb, GetX(0) + pos, aTop + aHeight + 20)
                End If

                Select Case (dtT - dtF).TotalSeconds
                    Case Is >= 2700
                        g.DrawString("分钟(×4)", Me.Font, Brushes.Gray, GetX(750), aTop + aHeight + 5)
                    Case Is >= 1800
                        g.DrawString("分钟(×2)", Me.Font, Brushes.Gray, GetX(750), aTop + aHeight + 5)
                    Case Is >= 600
                        g.DrawString("分钟(×1)", Me.Font, Brushes.Gray, GetX(750), aTop + aHeight + 5)
                    Case Else
                        g.DrawString("秒(×30)", Me.Font, Brushes.Gray, GetX(750), aTop + aHeight + 5)
                End Select
        End Select



        p.Dispose()
        p1.Dispose()

    End Sub

    Private Sub DrawYCoordinate(ByVal g As Graphics)
        Dim gap As Integer = 40

        Dim x As Integer = aLeft
        Dim iRow As Integer = 0
        Dim index As Integer = 0
        If hasTemp Then

            index += 1

            x -= gap
            Dim p As New Pen(Color.Orange)
            Dim b As New SolidBrush(Color.Orange)

            Dim yd As Integer = 0

            g.DrawLine(p, x, aTop + yd - 15, x, aTop + aHeight + yd)
            g.FillPolygon(New SolidBrush(p.Color), New Point() {New Point(x, aTop + yd - 15), _
                                                                New Point(x - 3, aTop + yd - 5), _
                                                                New Point(x + 3, aTop + yd - 5) _
                                                    } _
                            )
            g.DrawString("℃", Me.Font, b, x + 5, aTop + yd - 15)

            g.DrawLine(p, x, aTop + yd, x + gap, aTop + yd)
            g.DrawLine(p, x, aTop + aHeight + yd, x + gap, aTop + aHeight + yd)

            'g.DrawString("℃", Me.Font, New SolidBrush(p.Color), GetX(770), aTop - 15)

            'If index Mod 2 = 0 Then
            '    yd = -12
            'End If

            Dim kd As Integer = aHeight / SCALECOUNT
            Dim s As String
            For k As Integer = 0 To SCALECOUNT
                s = Math.Round(MAX_TEMP_LIMIT - k * (MAX_TEMP_LIMIT - MIN_TEMP_LIMIT) / SCALECOUNT, 2)
                g.DrawString(s, Me.Font, b, x, aTop + k * kd + yd)
                g.DrawLine(p, x - 2, aTop + k * kd, x + 2, aTop + k * kd)
            Next

            '**************************************************************
            '20090616 李吉鹏增加 为增加温湿度基准线及控制显示功能
            If TEMP_UP_LINE_SHOW Then
                Me.TempUpLine.Location = New Point(aLeft + Me.PictureBox1.Location.X, (1 - (TEMP_UP_LINE - MIN_TEMP_LIMIT) / (MAX_TEMP_LIMIT - MIN_TEMP_LIMIT)) * aHeight + Me.PictureBox1.Location.Y + aTop)
                Me.TempUpLine.Visible = True
                g.DrawString("温度基准上限", Me.Font, b, aLeft + Me.PictureBox1.Location.X + aWidth, (1 - (TEMP_UP_LINE - MIN_TEMP_LIMIT) / (MAX_TEMP_LIMIT - MIN_TEMP_LIMIT)) * aHeight + Me.PictureBox1.Location.Y + aTop)
            Else : Me.TempUpLine.Visible = False

            End If

            If TEMP_DOWN_LINE_SHOW Then
                Me.TempDownLine.Location = New Point(aLeft + Me.PictureBox1.Location.X, (1 - (TEMP_DOWN_LINE - MIN_TEMP_LIMIT) / (MAX_TEMP_LIMIT - MIN_TEMP_LIMIT)) * aHeight + Me.PictureBox1.Location.Y + aTop)
                Me.TempDownLine.Visible = True
                g.DrawString("温度基准下限", Me.Font, b, aLeft + Me.PictureBox1.Location.X + aWidth, (1 - (TEMP_DOWN_LINE - MIN_TEMP_LIMIT) / (MAX_TEMP_LIMIT - MIN_TEMP_LIMIT)) * aHeight + Me.PictureBox1.Location.Y + aTop)
            Else : Me.TempDownLine.Visible = False

            End If
            '**************************************************************************

            p.Dispose()
            b.Dispose()

        End If

        If hasHumi Then

            index += 1

            x -= gap
            Dim p As New Pen(Color.LightBlue)
            Dim b As New SolidBrush(Color.LightBlue)

            Dim yd As Integer = 0

            g.DrawLine(p, x, aTop + yd - 15, x, aTop + aHeight + yd)
            g.FillPolygon(New SolidBrush(p.Color), New Point() {New Point(x, aTop + yd - 15), _
                                                                New Point(x - 3, aTop + yd - 5), _
                                                                New Point(x + 3, aTop + yd - 5) _
                                                    } _
                            )
            g.DrawString("%", Me.Font, b, x + 5, aTop + yd - 15)

            g.DrawLine(p, x, aTop + yd, x + gap, aTop + yd)
            g.DrawLine(p, x, aTop + aHeight + yd, x + gap, aTop + aHeight + yd)

            'g.DrawString("％", Me.Font, New SolidBrush(p.Color), GetX(750), aTop - 15)

            'If index Mod 2 = 0 Then
            '    yd = -12
            'End If

            Dim kd As Integer = aHeight / SCALECOUNT
            Dim s As String
            For k As Integer = 0 To SCALECOUNT
                s = Math.Round(MAX_HUM_LIMIT - k * (MAX_HUM_LIMIT - MIN_HUM_LIMIT) / SCALECOUNT, 2)
                g.DrawString(s, Me.Font, b, x, aTop + k * kd + yd)
                g.DrawLine(p, x - 2, aTop + k * kd, x + 2, aTop + k * kd)
            Next
            '**************************************************************
            '20090616 李吉鹏增加 为增加温湿度基准线及控制显示功能
            If HUMI_UP_LINE_SHOW Then
                Me.HumiUpLine.Location = New Point(aLeft + Me.PictureBox1.Location.X, (1 - (HUMI_UP_LINE - MIN_HUM_LIMIT) / (MAX_HUM_LIMIT - MIN_HUM_LIMIT)) * aHeight + Me.PictureBox1.Location.Y + aTop)
                Me.HumiUpLine.Visible = True
                g.DrawString("湿度基准上限", Me.Font, b, aLeft + Me.PictureBox1.Location.X + aWidth, (1 - (HUMI_UP_LINE - MIN_HUM_LIMIT) / (MAX_HUM_LIMIT - MIN_HUM_LIMIT)) * aHeight + Me.PictureBox1.Location.Y)
            Else : Me.HumiUpLine.Visible = False

            End If

            If HUMI_DOWN_LINE_SHOW Then
                Me.HumiDownLine.Location = New Point(aLeft + Me.PictureBox1.Location.X, (1 - (HUMI_DOWN_LINE - MIN_HUM_LIMIT) / (MAX_HUM_LIMIT - MIN_HUM_LIMIT)) * aHeight + Me.PictureBox1.Location.Y + aTop)
                Me.HumiDownLine.Visible = True
                g.DrawString("湿度基准下限", Me.Font, b, aLeft + Me.PictureBox1.Location.X + aWidth, (1 - (HUMI_DOWN_LINE - MIN_HUM_LIMIT) / (MAX_HUM_LIMIT - MIN_HUM_LIMIT)) * aHeight + Me.PictureBox1.Location.Y)
            Else : Me.HumiDownLine.Visible = False
                '*****************************************************************************
            End If
            p.Dispose()
            b.Dispose()

        End If

        Dim y As Integer = 0
        For i As Integer = 1 To SCALECOUNT - 1
            y += Me.aHeight / SCALECOUNT
            'g.DrawLine(Me._forePen, aLeft, aTop + y, aLeft + aWidth, aTop + y)
            g.DrawLine(Pens.Gray, x, aTop + y, aLeft + aWidth, aTop + y)
        Next

    End Sub

    Private Function GetX(ByVal x As Integer) As Integer
        'System.Diagnostics.Debug.WriteLine("x坐标: " + x.ToString)
        Return Me.aLeft + x

    End Function

    'Public Function GetY3(ByVal y As Single, ByVal r As RTDValueInfo) As Integer
    '    Dim rn As New Random
    '    'y = rn.Next(1, 120)
    '    Dim t As Integer
    '    t = aHeight + aTop - ((y - r.minValue) / (r.maxValue - r.minValue)) * aHeight '- (r.maxValue + r.minValue) / 2
    '    Return t
    '    'Return 10 + y / 20
    'End Function

    Public Function GetY(ByVal y As Single, ByVal r As RTDValueInfo) As Integer
        Dim rn As New Random
        'y = rn.Next(1, 120)

        Dim t As Integer

        'todo:y值无效
        y = y Mod 20000

        Try
            t = aHeight + aTop - ((y - r.minValue) / (r.maxValue - r.minValue)) * aHeight '- (r.maxValue + r.minValue) / 2
        Catch ex As Exception
            Debug.WriteLine(ex.ToString)
            Throw ex
        End Try


        'Debug.WriteLine(r.Name & ": " & y & " - " & t)
        Return t
        'Return 10 + y / 20
    End Function


    Public Function GetY(ByVal y As Single) As Integer
        Return 10 + y / 20
    End Function

    '获取终止时间
    Private Function GetCurrentTime() As DateTime
        Return ddtpTo
    End Function

    Private Sub DrawLine(ByVal g As Graphics)
        Dim SwitchCount As Integer = 0
        Dim count As Integer = 0
        For Each d As RTData In _dl 'd代表存放端口一个端口信息和端口对应着一段时间的相关数据信息

            If d.rtdItem.length = 0 Then
                '开关量
                SwitchCount += 1
                Dim yy As Integer = Me.aTop + Me.aHeight - SwitchCount * 12

                Dim p As New Pen(d.rtdItem.color, 10)
                Dim p2 As New Pen(d.rtdItem.color, 1)
                For i As Integer = 1 To d.CurrentIndex
                    If d._bufByte(i) Mod 2 = 0 Then
                        g.DrawLine(p2, GetX(i - 1), yy, GetX(i), yy)
                    Else
                        g.DrawLine(p, GetX(i - 1), yy, GetX(i), yy)
                    End If
                Next
                p.Dispose()
                p2.Dispose()

            Else
                '曲线

                count = 0
                Dim p As New Pen(d.rtdItem.color)
                Dim pv, cv As Single
                'Dim ii As Integer = 0
                Dim endPoint As Single '存放最后一点
                For i As Integer = 1 To d._bufSingle.Length - 1

                    'If i = 360 Then  '测试用代码
                    '    i = i
                    'End If

                    If i = 1 Then
                        endPoint = d._bufSingle(0)
                    End If


                    If Not (Single.IsNaN(d._bufSingle(i))) Then

                        If endPoint = 0.0 Then
                            endPoint = d._bufSingle(i)
                        End If
                        pv = endPoint '代表端口时间的一个数据点

                        cv = d._bufSingle(i) '上面的下一个点

                        If Not Single.IsNaN(d._bufSingle(i)) Then


                            If (-300) = pv Or (-300) = cv Then
                            Else
                                endPoint = d._bufSingle(i)
                                If pv <= d.rtdItem.maxValue Then
                                    If pv >= d.rtdItem.minValue Then
                                        If cv <= d.rtdItem.maxValue Then
                                            If cv >= d.rtdItem.minValue Then
                                                count += 1
                                                _g.DrawLine(p, GetX(i - 1), GetY(pv, d.rtdItem), GetX(i), GetY(cv, d.rtdItem))
                                            Else
                                                count += 1
                                                _g.DrawLine(p, GetX(i - 1), GetY(pv, d.rtdItem), GetX(i), GetY(Math.Max(cv, d.rtdItem.minValue), d.rtdItem))
                                            End If
                                        Else
                                            count += 1
                                            _g.DrawLine(p, GetX(i - 1), GetY(pv, d.rtdItem), GetX(i), GetY(Math.Min(cv, d.rtdItem.maxValue), d.rtdItem))
                                        End If
                                    Else
                                        If cv >= d.rtdItem.minValue Then
                                            count += 1
                                            _g.DrawLine(p, GetX(i - 1), GetY(Math.Max(pv, d.rtdItem.minValue), d.rtdItem), GetX(i), GetY(cv, d.rtdItem))
                                        End If
                                    End If
                                Else
                                    If cv <= d.rtdItem.maxValue Then
                                        If cv >= d.rtdItem.minValue Then
                                            count += 1
                                            _g.DrawLine(p, GetX(i - 1), GetY(Math.Min(pv, d.rtdItem.maxValue), d.rtdItem), GetX(i), GetY(cv, d.rtdItem))
                                        Else
                                            count += 1
                                            _g.DrawLine(p, GetX(i - 1), GetY(Math.Min(pv, d.rtdItem.maxValue), d.rtdItem), GetX(i), GetY(Math.Max(cv, d.rtdItem.minValue), d.rtdItem))
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                Next i
                p.Dispose()
            End If
            System.Diagnostics.Debug.WriteLine("端口-AI" + d.rtdItem.ID.ToString() + " 共绘画了" + count.ToString() + "点")
        Next

    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'If 1 = 1 Then
        '    Exit Sub
        'End If

        'Dim strSql As String

        'strSql = "Delete From tbccHDData"
        'SqlHelper.ExecuteNonQuery(strSql, Nothing)

        'Dim dt As DateTime = Now.ToLongDateString()

        'Dim tic As New TBCC.Utility.TranstrationItem()

        'For i As Integer = 0 To 2000
        '    strSql = "Insert Into tbccHDData(hdate,AI1,AI2,AI3,AI4,AI5,AI6,AI7,AI8,AI9,AI10,AI11,AI12,AI13,AI14,AI15,AI16,DO1,DO2,DO3,DO4,DO5,DO6) Values(?,?,?,?,?,  ?,?,?,?,?,  ?,?,?,?,?,  ?,?,1,1,1,  0,0,1) --" & i.ToString()

        '    Dim p(16) As MySql.Data.MySqlClient.MySqlParameter
        '    For j As Integer = 0 To p.Length - 1
        '        p(j) = New MySql.Data.MySqlClient.MySqlParameter()
        '    Next

        '    p(0).Value = UtilityClass.GetDateString(dt.AddMinutes(i * 2))

        '    For k As Integer = 1 To 16
        '        p(k).Value = g_rand.Next(20 - k, 50 + k * 2)
        '    Next

        '    tic.Add(strSql, p)
        'Next

        'SqlHelper.ExecuteTranstration(tic)


    End Sub

    Private Sub dtpFrom_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
    Dim SaveColorType As Integer = -1

    Private Sub btnSavePic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSavePic.Click
        Dim cs As New ColorSelect()
        If cs.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
            Exit Sub
        End If

        SaveColorType = cs.Type
        Dim dlg As New SaveFileDialog
        dlg.Filter = "BMP (*.bmp;*.dib)|*.bmp;*.dib|" & _
                     "JPEG (*.JPG;*.JPEG;*.JPE;*.JFIF)|*.JPG;*.JPEG;*.JPE;*.JFIF|" & _
                     "GIF (*.GIF)|*.Gif|" & _
                     "TIFF (*.TIF;*.TIFF)|*.TIF;*.TIFF|" & _
                     "PNG (*.PNG)|*.Png"
        dlg.FileName = Now.ToString("yyyyMMdd") '& ".bmp"
        dlg.FilterIndex = 2
        If dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim format As System.Drawing.Imaging.ImageFormat
            Select Case dlg.FilterIndex
                Case 0
                    format = System.Drawing.Imaging.ImageFormat.Bmp
                Case 1
                    format = System.Drawing.Imaging.ImageFormat.Jpeg
                Case 2
                    format = System.Drawing.Imaging.ImageFormat.Gif
                Case 3
                    format = System.Drawing.Imaging.ImageFormat.Tiff
                Case 4
                    format = System.Drawing.Imaging.ImageFormat.Png
                Case Else
                    format = System.Drawing.Imaging.ImageFormat.Jpeg
            End Select

            RefreshImage4Print()
            Me._bmpPrint.Save(dlg.FileName, format)
            'Me.PictureBox1.Image.Save(dlg.FileName, System.Drawing.Imaging.ImageFormat.Bmp)
        End If
    End Sub


#Region "Print"

    Private Sub InitBG4Print(ByVal g As Graphics)
        Select Case SaveColorType
            Case 0
                _foreBrush4Print.Color = Color.White
                _forePen4Print.Color = Color.White
                g.Clear(Color.Black)
                DrawMark4Print(g)
                DrawCoordinates4Print(g)
                DrawXCoordinate(g, False)
                DrawYCoordinate(g)
            Case Else
                _foreBrush4Print.Color = Color.Black
                _forePen4Print.Color = Color.Black

                g.Clear(Color.White)
                DrawMark4Print(g)
                DrawCoordinates4Print(g)
                DrawXCoordinate(g, True)
                DrawYCoordinate(g)
        End Select
    End Sub

    ''' <summary>
    ''' 画曲线(打印用)
    ''' </summary>
    ''' <param name="g"></param>
    ''' <remarks></remarks>
    Private Sub DrawLine4Print(ByVal g As Graphics)
        Dim SwitchCount As Integer = 0
        For Each d As RTData In _dl
            If d.rtdItem.length = 0 Then
                '开关量
                SwitchCount += 1
                Dim yy As Integer = Me.aTop + Me.aHeight - SwitchCount * 12

                Dim p As New Pen(d.rtdItem.color, 10)
                Dim p2 As New Pen(d.rtdItem.color, 1)
                For i As Integer = 1 To d.CurrentIndex
                    If d._bufByte(i) Mod 2 = 0 Then
                        g.DrawLine(p2, GetX(i - 1), yy, GetX(i), yy)
                    Else
                        g.DrawLine(p, GetX(i - 1), yy, GetX(i), yy)
                    End If
                Next
                p.Dispose()
                p2.Dispose()

            Else
                '曲线

                Dim p As New Pen(d.rtdItem.color)
                Dim pv, cv As Single
                For i As Integer = 1 To d._bufSingle.Length - 1
                    ''CurrentIndex=0 ??????
                    'If Not (Single.IsNaN(d._bufSingle(i - 1)) OrElse Single.IsNaN(d._bufSingle(i))) Then
                    '    g.DrawLine(p, GetX(i - 1), GetY(d._bufSingle(i - 1), d.rtdItem), GetX(i), GetY(d._bufSingle(i), d.rtdItem))
                    'End If
                    If Not (Single.IsNaN(d._bufSingle(i - 1)) OrElse Single.IsNaN(d._bufSingle(i))) Then
                        'ii += 1
                        pv = d._bufSingle(i - 1)
                        cv = d._bufSingle(i)
                        'If pv >= d.rtdItem.minValue AndAlso pv <= d.rtdItem.maxValue Then



                        If pv <= d.rtdItem.maxValue Then
                            If pv >= d.rtdItem.minValue Then
                                If cv <= d.rtdItem.maxValue Then
                                    If cv >= d.rtdItem.minValue Then
                                        g.DrawLine(p, GetX(i - 1), GetY(pv, d.rtdItem), GetX(i), GetY(cv, d.rtdItem))
                                    Else
                                        g.DrawLine(p, GetX(i - 1), GetY(pv, d.rtdItem), GetX(i), GetY(Math.Max(cv, d.rtdItem.minValue), d.rtdItem))
                                    End If
                                Else
                                    g.DrawLine(p, GetX(i - 1), GetY(pv, d.rtdItem), GetX(i), GetY(Math.Min(cv, d.rtdItem.maxValue), d.rtdItem))
                                End If
                            Else
                                If cv >= d.rtdItem.minValue Then
                                    g.DrawLine(p, GetX(i - 1), GetY(Math.Max(pv, d.rtdItem.minValue), d.rtdItem), GetX(i), GetY(cv, d.rtdItem))
                                End If
                            End If
                        Else
                            If cv <= d.rtdItem.maxValue Then
                                If cv >= d.rtdItem.minValue Then
                                    g.DrawLine(p, GetX(i - 1), GetY(Math.Min(pv, d.rtdItem.maxValue), d.rtdItem), GetX(i), GetY(cv, d.rtdItem))
                                Else
                                    g.DrawLine(p, GetX(i - 1), GetY(Math.Min(pv, d.rtdItem.maxValue), d.rtdItem), GetX(i), GetY(Math.Max(cv, d.rtdItem.minValue), d.rtdItem))
                                End If
                            End If
                        End If
                    End If
                    'g.DrawLine(p, GetX(i - 1), GetY(d._bufSingle(i - 1), d.rtdItem), GetX(i), GetY(d._bufSingle(i), d.rtdItem))
                    'End If

                Next
                p.Dispose()
            End If
        Next
    End Sub

    Private Sub DrawCoordinates4Print(ByVal g As Graphics)
        Dim r As New Rectangle(aLeft, aTop, aWidth, aHeight)
        g.DrawRectangle(_forePen4Print, r)
        g.DrawLine(_forePen4Print, aLeft + aWidth, aTop + aHeight, aLeft + aWidth + 15, aTop + aHeight)
        g.FillPolygon(New SolidBrush(_forePen4Print.Color), New Point() {New Point(aLeft + aWidth + 15, aTop + aHeight), _
                                                                New Point(aLeft + aWidth + 5, aTop + aHeight + 3), _
                                                                New Point(aLeft + aWidth + 5, aTop + aHeight - 3) _
                                                    } _
                            )
    End Sub

    Private Sub DrawMark4Print(ByVal g As Graphics)
        Dim markTop As Integer = 25
        Dim height As Integer = 25
        Dim left As Integer = aLeft + aWidth + 40

        Dim rMark As New Rectangle(left, aTop, 120, height * _l.Count)
        g.DrawRectangle(_forePen4Print, rMark)

        Dim font As Font = New Font(Me.Font.FontFamily, 10)

        Dim iRow As Integer = 0
        For Each item As RTDValueInfo In _l
            Dim r As New Rectangle(left + 20, iRow * height + markTop + 1, 12, 12)
            Dim b As Brush = New SolidBrush(item.color)
            g.FillRectangle(b, r)
            Dim s As String
            s = item.Name
            g.DrawString(s, font, _foreBrush4Print, left + 50, iRow * height + markTop)
            iRow += 1
        Next
    End Sub
#End Region

    Private Delegate Sub VoidMethodHandler(ByVal obj As Object)
    Private Sub bwSearch_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bwSearch.DoWork
        Dim obj As Object = Nothing

        Dim str As String = String.Format("正在生成车载历史曲线……")
        If Me.InvokeRequired Then
            Me.Invoke(New VoidMethodHandler(AddressOf SetSearchState), str)
        Else
            SetSearchState(str)
        End If

        RefreshData(e.Argument)

        If Me.InvokeRequired Then
            Me.Invoke(New VoidMethodHandler(AddressOf RefreshImageMethod), obj)
        Else
            RefreshImage()
        End If

    End Sub

    Private Sub bwSearch_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bwSearch.ProgressChanged
        Me.pbSearch.Value = e.ProgressPercentage
    End Sub

    Private Sub bwSearch_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bwSearch.RunWorkerCompleted
        ' Me.cboWarehouse.Enabled = True
        ' Me.dtpFrom.Enabled = False
        'Me.dtpTo.Enabled = False
        Me.btnSavePic.Enabled = True
        Me.btnTHCCfg.Enabled = True
        '   Me.btnRefresh.Enabled = True
        '    Me.btnRefresh.Text = "刷新"
        Me.pnlSearch.Visible = False
        Me.PictureBox1.Focus()
        Me.dtpFrom.Enabled = True
        Me.dtpTo.Enabled = True
        Me.btnShow.Enabled = True
        Me.btnTHCCfg.Enabled = True
        Me.btnSavePic.Enabled = True
        Me.btnClose.Enabled = True

        GC.Collect()
    End Sub
    Private Sub RefreshImageMethod(ByVal obj As Object)
        RefreshImage()
    End Sub
    Private Sub ReportProgress(ByVal value As Integer)
        If Not Me.IsDisposed Then
            If Me.InvokeRequired Then
                Me.Invoke(New VoidMethodHandler(AddressOf ReportProgressMethod), value)
            Else
                ReportProgressMethod(value)
            End If
        End If
    End Sub
    Private Sub ReportProgressMethod(ByVal obj As Object)
        Me.bwSearch.ReportProgress(obj)
    End Sub
    Private Sub SetSearchState(ByVal obj As Object)
        Me.lblSearchState.Text = obj
    End Sub

#Region "hiden"

    'Dim clickPoint, startPoint, endPoint As Point
    'Dim selectedSection As New Rectangle(0, 0, 0, 0)
    'Dim clickBmp As Bitmap
    'Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    '    clickPoint = e.Location
    '    startPoint = clickPoint
    'End Sub

    'Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
    '    If clickPoint <> Nothing Then
    '        If e.Location.X > clickPoint.X Then
    '            If e.Location.Y > clickPoint.Y Then
    '                startPoint = clickPoint
    '                endPoint = e.Location
    '            Else
    '                startPoint = New Point(clickPoint.X, e.Y)
    '                endPoint = New Point(e.X, clickPoint.Y)
    '            End If
    '        Else
    '            If e.Location.Y > clickPoint.Y Then
    '                startPoint = New Point(e.X, clickPoint.Y)
    '                endPoint = New Point(clickPoint.X, e.Y)
    '            Else
    '                startPoint = e.Location
    '                endPoint = clickPoint
    '            End If
    '        End If

    '        selectedSection.Location = startPoint
    '        selectedSection.Size = Size_Abs(startPoint - endPoint)

    '        InitBG(_g)
    '        RefreshImage()
    '        _g.DrawRectangle(Pens.Yellow, selectedSection)

    '        Me.PictureBox1.Image = _bmp
    '    End If
    'End Sub

    'Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
    '    clickPoint = Nothing
    '    InitBG(_g)
    '    RefreshImage()
    '    Me.PictureBox1.Image = _bmp
    'End Sub

    'Private Function Size_Abs(ByVal size As Size) As Size
    '    size.Height = Math.Abs(size.Height)
    '    size.Width = Math.Abs(size.Width)

    '    Return size
    'End Function

#End Region

    Private Sub UpdateRTDValueInfo(ByVal dataType As Integer)
        For Each vi As RTDValueInfo In _l
            If vi.dataType = dataType Then
                Select Case dataType
                    Case 1
                        vi.minValue = MIN_TEMP_LIMIT
                        vi.maxValue = MAX_TEMP_LIMIT
                    Case 2
                        vi.minValue = MIN_HUM_LIMIT
                        vi.maxValue = MAX_HUM_LIMIT
                End Select
            End If
        Next
    End Sub

#Region "刻度线"

    Private tempBase, humiBase, timeBase As Single
    Private tempEnd, humiEnd As Integer
    Private timeStart As DateTime
    Private strLabCur As String
    Private strLabInfo As String
    Private curDateTime As DateTime
    Private curInfo(11) As Single

    Private Sub SetCurLabPara()
        tempEnd = MAX_TEMP_LIMIT
        humiEnd = MAX_HUM_LIMIT
        If (Me.dtpTo.Value - Me.dtpFrom.Value).TotalSeconds >= 3600 Then
            timeStart = ddtpFrom
        Else
            timeStart = Me.dtpFrom.Value
        End If

        tempBase = (MAX_TEMP_LIMIT - MIN_TEMP_LIMIT) / (aHeight)
        humiBase = (MAX_HUM_LIMIT - MIN_HUM_LIMIT) / (aHeight)
        If (Me.dtpTo.Value - Me.dtpFrom.Value).TotalSeconds >= 3600 Then
            timeBase = (ddtpTo - ddtpFrom).TotalSeconds / (720)
        Else
            timeBase = (Me.dtpTo.Value - Me.dtpFrom.Value).TotalSeconds / (720)

        End If

        strLabCur = "日期：{0}" & vbCrLf
        strLabCur &= "时间：{1}" & vbCrLf

        If hasTemp Then
            strLabCur &= "温度：{2:F2} ℃" & vbCrLf
        End If

        If hasHumi Then
            strLabCur &= "湿度：{3:F2} %" & vbCrLf
        End If
    End Sub

    Private Sub SetInfoLabPara()
        Dim index As Integer = 2

        strLabInfo = "日期：{0}" & vbCrLf
        strLabInfo &= "时间：{1}" & vbCrLf

        For Each item As RTDValueInfo In _l
            Select Case item.dataType
                Case 1
                    strLabInfo &= item.Name & "：{" & index & ":F2} ℃" & vbCrLf
                Case 2
                    strLabInfo &= item.Name & "：{" & index & ":F2} %" & vbCrLf
            End Select
            index += 1
        Next
    End Sub

    Private Sub FreshCurInfo(ByVal i As Integer)
        Dim index As Integer = 0

        For Each item As RTData In _dl
            curInfo(index) = item._bufSingle(i)
            index += 1
        Next
    End Sub

    'mouse
    Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        'System.Diagnostics.Debug.WriteLine(1.ToString())
        If Me.pnlSearch.Visible = False Then
            If e.X >= aLeft AndAlso e.Y >= aTop AndAlso e.X <= aLeft + 720 AndAlso e.Y <= aTop + aHeight Then
                Dim curDT As DateTime
                Dim curDate, curTime As String

                curDT = timeStart.AddSeconds((e.X - aLeft) * timeBase)
                curDate = curDT.ToShortDateString()
                'curDate = FormatDataTime30(curDT) 
                curTime = FormatDataTime30(curDT) '格式化时间

                '指针位置与信息
                labCur.Text = String.Format(strLabCur, _
                                            curDate, _
                                            curTime, _
                                            Math.Round(tempEnd - (e.Y - aTop) * tempBase, 2), _
                                            Math.Round(humiEnd - (e.Y - aTop) * humiBase, 2) _
                                            )
                labCur.Location = New Point(e.X + 15, e.Y + 15)
                labCur.Visible = True

                '刻度线信息
                If e.Button = Windows.Forms.MouseButtons.Left Then
                    keyIndex = GetNearKIL(e.X - aLeft)
                    FreshCurInfo(keyIndex)
                    labInfo.Text = String.Format(strLabInfo, curDate, curTime, curInfo(0), curInfo(1), curInfo(2), curInfo(3), curInfo(4), curInfo(5), _
                                                                      curInfo(6), curInfo(7), curInfo(8), curInfo(9), curInfo(10), curInfo(11))
                    'labLine.Location = New Point(e.X + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)
                    labLine.Location = New Point(aLeft + keyIndex + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)
                    labLine.Visible = True
                End If

                '刻度线位置
                If labLine.Visible = True Then
                    'labInfo.Location = New Point(labCur.Location.X, labCur.Location.Y + labCur.Size.Height + 5)
                    labInfo.Location = New Point(aLeft + 735, aTop + aHeight - labInfo.Size.Height)
                    labInfo.Visible = True
                End If

            Else
                labCur.Visible = False
                If e.Button = Windows.Forms.MouseButtons.Left Then
                    labLine.Visible = False
                    labInfo.Visible = False
                End If
            End If
        End If

    End Sub
    '格式化时间
    Private Function FormatDataTime30(ByVal tm As DateTime) As String

        If _timeInterval Then '在720点块内
            Return tm.ToLongTimeString
        Else '在720点块外
            Dim h As Integer = tm.Hour
            Dim m As Integer = tm.Minute
            Dim s As Integer = tm.Second

            If s < 45 And s >= 15 Then
                s = 30
            ElseIf (s >= 45) And (m < 60) Then
                s = 0
                m += 1
            ElseIf (s >= 0 And s < 15) Then
                s = 0
            End If


            Dim hh As String = h.ToString
            Dim mm As String = m.ToString
            Dim ss As String = s.ToString


            If CInt(hh) < 10 Then
                hh = "0" + hh
            End If

            If CInt(mm) < 10 And CInt(mm) < 59 Then
                mm = "0" + mm
            End If

            If CInt(ss) < 10 Then
                ss = "0" + ss
            End If

            Dim endTime As String = hh + ":" + mm + ":" + ss
            Return endTime

        End If

    End Function

    Private Sub PictureBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
        System.Diagnostics.Debug.WriteLine(3.ToString())
        Me.PictureBox1.Focus()

        If Me.pnlSearch.Visible = False Then
            If e.X >= aLeft AndAlso e.Y >= aTop AndAlso e.X <= aLeft + 720 AndAlso e.Y <= aTop + aHeight Then

                curDateTime = timeStart.AddSeconds((e.X - aLeft) * timeBase)

                '刻度线位置与信息
                keyIndex = GetNearKIL(e.X - aLeft)
                FreshCurInfo(keyIndex)
                FreshLabInfo(keyIndex)
                'labInfo.Text = String.Format(strLabInfo, curDateTime.ToShortDateString, FormatDataTime30(curDateTime), _
                '                                                      curInfo(0), curInfo(1), curInfo(2), curInfo(3), curInfo(4), curInfo(5), _
                '                                                      curInfo(6), curInfo(7), curInfo(8), curInfo(9), curInfo(10), curInfo(11))
                'labLine.Location = New Point(e.X + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)
                labLine.Location = New Point(aLeft + keyIndex + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)

                'labInfo.Location = New Point(labCur.Location.X, labCur.Location.Y + labCur.Size.Height + 5)
                labInfo.Location = New Point(aLeft + 735, aTop + aHeight - labInfo.Size.Height)
                labLine.Visible = True
                labInfo.Visible = True

            Else
                labLine.Visible = False
                labInfo.Visible = False
            End If
        End If

    End Sub

#Region "光标位置"
    'keyboard
    Private keyIndex As Integer
    Private KeyIndexList As New LinkedList(Of Integer) '一个队列,存放有数据的节点位置
    Private KeyIndexMnN As New LinkedList(Of Integer)
    Private IsFirst As Boolean                     'DLine方法会被执行4次, 只需要往队列里添加一次坐标就行了, 这个作为标识 确认是不是第一次添加

    ''' <summary>
    ''' 初始化有数据的位置队列
    ''' </summary>
    ''' <param name="Index">位置</param>
    ''' <remarks></remarks>
    Private Sub AddKIL(ByVal Index As Integer)
        KeyIndexList.AddLast(Index)
    End Sub

    ''' <summary>
    ''' 按下left时返回的KeyIndex值
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function ForwardKIL() As Integer
        KeyIndexList.AddFirst(KeyIndexList.Last.Value)
        KeyIndexList.RemoveLast()
        Return KeyIndexList.First.Value
    End Function

    Private Function BackwardKIL() As Integer
        KeyIndexList.AddLast(KeyIndexList.First.Value)
        KeyIndexList.RemoveFirst()
        Return KeyIndexList.First.Value
    End Function

    Private Function GetNearKIL(ByVal pos As Integer) As Integer
        If KeyIndexList.Count = 0 Then Return pos
        If KeyIndexMnN.Count <= 0 Then
            Return pos
        End If
        If pos < KeyIndexMnN.First.Value Then
            While KeyIndexList.First.Value <> KeyIndexMnN.First.Value
                ForwardKIL()
            End While
            Return KeyIndexList.First.Value
        End If
        If pos > KeyIndexMnN.Last.Value Then
            While KeyIndexList.First.Value <> KeyIndexMnN.Last.Value
                ForwardKIL()
            End While
            Return KeyIndexList.First.Value
        End If
        If KeyIndexList.First.Value <= pos And KeyIndexList.First.Next.Value >= pos Then
            If pos - KeyIndexList.First.Value <= KeyIndexList.First.Next.Value - pos Then
                Return KeyIndexList.First.Value
            Else
                Return BackwardKIL()
            End If
        End If
        Do
            BackwardKIL()
        Loop While IsNotBetween(pos)
        If pos - KeyIndexList.First.Value <= KeyIndexList.First.Next.Value - pos Then
            Return KeyIndexList.First.Value
        Else
            Return BackwardKIL()
        End If
    End Function

    Private Function IsNotBetween(ByVal pos As Integer) As Boolean
        If KeyIndexList.First.Value <= pos And KeyIndexList.First.Next.Value >= pos Then
            Return False
        Else
            Return True
        End If
    End Function

#End Region


    Private Sub PictureBox1_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles PictureBox1.PreviewKeyDown
        Select Case e.KeyCode
            Case Keys.Left
                LabLine_Forward(1)

            Case Keys.Right
                LabLine_Backward(1)

            Case Keys.Up
                LabLine_Forward(5)

            Case Keys.Down
                LabLine_Backward(5)

            Case Keys.PageUp
                LabLine_Forward(10)

            Case Keys.PageDown
                LabLine_Backward(10)

            Case Keys.Home
                While KeyIndexList.First.Value <> KeyIndexMnN.First.Value
                    ForwardKIL()
                End While
                keyIndex = KeyIndexList.First.Value
                FreshLabInfo(keyIndex)
                labLine.Location = New Point(aLeft + keyIndex + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)
                labInfo.Location = New Point(aLeft + 735, aTop + aHeight - labInfo.Size.Height)
                labLine.Visible = True
                labInfo.Visible = True

            Case Keys.End
                While KeyIndexList.First.Value <> KeyIndexMnN.Last.Value
                    ForwardKIL()
                End While
                keyIndex = KeyIndexList.First.Value
                FreshLabInfo(keyIndex)
                labLine.Location = New Point(aLeft + keyIndex + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)
                labInfo.Location = New Point(aLeft + 735, aTop + aHeight - labInfo.Size.Height)
                labLine.Visible = True
                labInfo.Visible = True

            Case Keys.Escape
                labLine.Visible = False
                labInfo.Visible = False

        End Select
        Dim dfd As MouseEventHandler = New MouseEventHandler(AddressOf Me.PictureBox1_MouseClick)
        '  dfd.Invoke(Me.PictureBox1, New MouseEventArgs(Windows.Forms.MouseButtons.Left, 1, 1, 1, 1))

    End Sub



    Private Sub FreshLabInfo(ByVal index As Integer)

        curDateTime = timeStart.AddSeconds((index) * timeBase)

        FreshCurInfo(index)                                                     'FormatDataTime30(curDateTime)
        labInfo.Text = String.Format(strLabInfo, curDateTime.ToShortDateString, vTime(index), _
                                                 curInfo(0), curInfo(1), curInfo(2), curInfo(3), curInfo(4), curInfo(5), _
                                                 curInfo(6), curInfo(7), curInfo(8), curInfo(9), curInfo(10), curInfo(11))

    End Sub

    Private Sub LabLine_Forward(ByVal [step] As Integer)
        If labLine.Visible = False Then
            keyIndex = ForwardKIL()
            FreshLabInfo(keyIndex)
            labLine.Location = New Point(aLeft + keyIndex + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)
            labInfo.Location = New Point(aLeft + 735, aTop + aHeight - labInfo.Size.Height)
            labLine.Visible = True
            labInfo.Visible = True
        Else
            ' If keyIndex > 0 Then
            ' keyIndex -= Math.Min([step], keyIndex)
            keyIndex = ForwardKIL()
            FreshLabInfo(keyIndex)
            labLine.Location = New Point(aLeft + keyIndex + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)
            ' Else
            ' keyIndex = 720
            ' FreshLabInfo(keyIndex)
            'labLine.Location = New Point(aLeft + keyIndex + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)
            'End If
        End If

    End Sub

    Private Sub LabLine_Backward(ByVal [step] As Integer)
        If labLine.Visible = False Then
            keyIndex = BackwardKIL()
            FreshLabInfo(keyIndex)
            labLine.Location = New Point(aLeft + keyIndex + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)
            labInfo.Location = New Point(aLeft + 735, aTop + aHeight - labInfo.Size.Height)
            labLine.Visible = True
            labInfo.Visible = True
        Else
            'If keyIndex < 720 Then
            'keyIndex += Math.Min([step], (720 - keyIndex))
            keyIndex = BackwardKIL()
            FreshLabInfo(keyIndex)
            labLine.Location = New Point(aLeft + keyIndex + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)
            ' Else
            ' keyIndex = 0
            ' FreshLabInfo(keyIndex)
            ' labLine.Location = New Point(aLeft + keyIndex + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)
            ' End If
        End If
    End Sub

#End Region

    Private Sub PictureBox1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDoubleClick
        System.Diagnostics.Debug.WriteLine(2.ToString())
        If (e.X >= _THC_Area.X AndAlso e.X <= (_THC_Area.X + _THC_Area.Width)) AndAlso (e.Y >= _THC_Area.Y AndAlso (e.Y <= _THC_Area.Y + _THC_Area.Height)) Then
            SetTHC()
        End If
    End Sub

    Private Sub btnTHCCfg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTHCCfg.Click
        SetTHC()
    End Sub

    ''' <summary>
    ''' '设置温湿度坐标区域
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub GetTHCArea()
        'x矩形左上角的 x 坐标。 
        'y矩形左上角的 y 坐标。 
        'Width矩形的宽度。 
        'Height矩形的高度。

        _THC_Area = New Rectangle(20, aTop, aLeft - 20, aHeight)
    End Sub

    Private Sub SetTHC()
        Dim attt As New CarRtiItemInfo
        attt = CarRtiItemService.GeRefGroupList(projectId).Item(0)
        Dim frm As New HDCurveLimitCfg(attt) 'cboWarehouse.SelectedItem)
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            'cboWarehouse.SelectedItem.HDCurveTempUp = frm.RefTypeItem.HDCurveTempUp
            'cboWarehouse.SelectedItem.HDCurveTempDown = frm.RefTypeItem.HDCurveTempDown
            'cboWarehouse.SelectedItem.HDCurveHumiUp = frm.RefTypeItem.HDCurveHumiUp
            'cboWarehouse.SelectedItem.HDCurveHumiDown = frm.RefTypeItem.HDCurveHumiDown

            'cboWarehouse.SelectedItem.HDCurveTempUp = frm.RefTypeItem.HDCurveTempLineUpShow
            MAX_TEMP_LIMIT = frm.RefTypeItem.HDCurveTempUp
            MIN_TEMP_LIMIT = frm.RefTypeItem.HDCurveTempDown
            MAX_HUM_LIMIT = frm.RefTypeItem.HDCurveHumiUp
            MIN_HUM_LIMIT = frm.RefTypeItem.HDCurveHumiDown
            '**************************************************************
            '20090616 李吉鹏增加 为增加温湿度基准线及控制显示功能
            TEMP_UP_LINE = frm.RefTypeItem.HDCurveTempLineUp
            TEMP_UP_LINE_SHOW = frm.RefTypeItem.HDCurveTempLineUpShow
            TEMP_DOWN_LINE = frm.RefTypeItem.HDCurveTempLineDown
            TEMP_DOWN_LINE_SHOW = frm.RefTypeItem.HDCurveTempLineDownShow

            HUMI_UP_LINE = frm.RefTypeItem.HDCurveHumiLineUp
            HUMI_UP_LINE_SHOW = frm.RefTypeItem.HDCurveHumiLineUpShow
            HUMI_DOWN_LINE = frm.RefTypeItem.HDCurveHumiLineDown
            HUMI_DOWN_LINE_SHOW = frm.RefTypeItem.HDCurveHumiLineDownShow
            '******************************************************************
            UpdateRTDValueInfo(1)
            UpdateRTDValueInfo(2)

            InitBG(_g) '刷新图层但不显示数据
            Refresh_Curve()
            Me.PictureBox1.Image = _bmp
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click

    End Sub

    Dim IsRefreshCur As Integer = 0
    Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
        Me.ddtpFrom = String.Format("{0} {1}:00:00", Me.dtpFrom.Value.ToString("yyyy-MM-dd"), Me.dtpFrom.Value.Hour)
        If Me.dtpTo.Value.Second = 0 And Me.dtpTo.Value.Minute = 0 Then
            Me.ddtpTo = String.Format("{0} {1}:00:00", Me.dtpTo.Value.ToString("yyyy-MM-dd"), Me.dtpTo.Value.Hour)
        Else
            If Me.dtpTo.Value.Hour = 23 Then
                Me.ddtpTo = String.Format("{0} 00:00:00", Me.dtpTo.Value.ToString("yyyy-MM-dd"))
                Me.ddtpTo = Me.ddtpTo.AddDays(1)
            Else
                Me.ddtpTo = String.Format("{0} {1}:00:00", Me.dtpTo.Value.ToString("yyyy-MM-dd"), Me.dtpTo.Value.Hour + 1)
            End If
        End If

        If IsRefreshCur < 4 Then
            IsRefreshCur += 1
            Exit Sub
        End If

        Refresh_Curve()

    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        btnShow_Click(sender, e)
    End Sub

    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
        btnShow_Click(sender, e)


    End Sub

    Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
        btnShow_Click(sender, e)


    End Sub

    Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
        btnShow_Click(sender, e)


    End Sub
End Class